package org.dromara.web.appInterface;

import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.app.appParams.AppLoginReqister;
import org.dromara.app.appParams.AppSendCode;
import org.dromara.app.appParams.AppWxLoginParam;
import org.dromara.app.appReturn.AppUserInfo;
import org.dromara.app.config.AppWeiXinProperties;
import org.dromara.app.cssUtils.CssUtils;
import org.dromara.app.service.ISkAppUserService;
import org.dromara.common.core.domain.R;
import org.dromara.system.service.ISysConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "登录相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/login")
@SaIgnore
public class AppLoginInterface {

    private final ISkAppUserService iSkAppUserService;

    private final AppWeiXinProperties appWeiXinProperties;

    private final ISysConfigService iSysConfigService;


    /**
     * 发送短信验证码
     */
    @PostMapping("/sendCode")
    @Operation(summary = "发送短信验证码", method = "post")
    public R sendCode(@RequestBody AppSendCode app){
        iSkAppUserService.sendCode(app);
        return R.ok("请求成功");
    }

    /**
     * 验证码登录注册接口
     * @param appReqister
     * @return
     */
    @PostMapping("/appLogin")
    @Operation(summary = "验证码登录注册接口", method = "post")
    public R<AppUserInfo> appLogin(@RequestBody AppLoginReqister appReqister){
        AppUserInfo app = iSkAppUserService.appLogin(appReqister);
        return R.ok(app);
    }

    /**
     * 微信登录  获取openId
     */
    @PostMapping("/getOpenIdByCode")
    @Operation(summary = "微信登录  获取openId", method = "post")
    public R<AppUserInfo> getOpenIdByCode(@RequestBody AppWxLoginParam appReqister){
       String openId =  CssUtils.getOpenId(appWeiXinProperties.getAppId(),appWeiXinProperties.getAppSecret(),appReqister.getCode());
        AppUserInfo app = iSkAppUserService.appLoginByOpenId(openId);
        return R.ok(app);
    }

    /**
     * 通过OPENiD 判断是否注册
     */
    @PostMapping("/getInfoByOpenId")
    @Operation(summary = "通过OPENiD 判断是否注册", method = "post")
    public R<AppUserInfo> getInfoByOpenId(@RequestBody AppWxLoginParam appReqister){
        AppUserInfo app = iSkAppUserService.appLoginByOpenId(appReqister.getOpenId());
        return R.ok(app);
    }

    /**
     * 获取第三方登录是否开启
     * @return
     */
    @PostMapping("/getOtherIsOpen")
    @Operation(summary = "获取第三方登录是否开启", method = "post")
    public R<String> getOtherIsOpen(){
        String isVip = iSysConfigService.selectConfigByKey("kai_tong_vip_an_niu");
        return R.ok(isVip);
    }



}
